    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.gui;

import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashSet;
import java.util.LinkedList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.SpringLayout;

import ch.zarzu.champions.builder.logic.Listener;

public class Description extends JLayeredPane implements MouseListener {
    private LinkedList<DescriptionBlock> description_blocks_list;
    private JButton lock;
    private String displayed_power;
    private SpringLayout layout;
    
    
    public Description() {
        displayed_power = null;
        
        layout = new SpringLayout();
        setLayout(layout);
        draw();
    }
    
    public void draw() {
        ImageIcon icon = new ImageIcon("images/misc/lock.png");
        lock = new JButton(icon);
        lock.setBorderPainted(false);
        lock.setFocusable(false);
        lock.setContentAreaFilled(false);
        lock.addMouseListener(this);
        lock.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
        lock.setVisible(false);
        
        description_blocks_list = new LinkedList<DescriptionBlock>();
        
        DescriptionBlock d;
        for(int i = 1; i < 6; i++) {
            d = new DescriptionBlock(this);
            description_blocks_list.add(d);
        }
        
        redraw();
    }
    
    public void redraw() {
        LinkedList<DescriptionBlock> tmp_description_blocks_list = new LinkedList<DescriptionBlock>();

        removeAll();
        
        add(lock);
        for(DescriptionBlock d : description_blocks_list) {
            if(d.isActive()) {
                add(d);
                tmp_description_blocks_list.add(d);
            }
        }
        
        layout.putConstraint(SpringLayout.EAST, lock, -1, SpringLayout.EAST, this);
        if(tmp_description_blocks_list.size() > 0) {
            layout.putConstraint(SpringLayout.EAST, tmp_description_blocks_list.get(0), 0, SpringLayout.EAST, this);
            layout.putConstraint(SpringLayout.WEST, tmp_description_blocks_list.get(0), 0, SpringLayout.WEST, this);
            layout.putConstraint(SpringLayout.NORTH, tmp_description_blocks_list.get(0), 0, SpringLayout.NORTH, this);
            for(int i = 1; i < tmp_description_blocks_list.size(); i++) {
                layout.putConstraint(SpringLayout.EAST, tmp_description_blocks_list.get(i), 0, SpringLayout.EAST, this);
                layout.putConstraint(SpringLayout.NORTH, tmp_description_blocks_list.get(i), 10, SpringLayout.SOUTH, tmp_description_blocks_list.get(i-1));
                layout.putConstraint(SpringLayout.WEST, tmp_description_blocks_list.get(i), 0, SpringLayout.WEST, this);
            }
            layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, tmp_description_blocks_list.get(tmp_description_blocks_list.size() - 1));
        }
    }
    
    public void setDescription(String id, LinkedList<LinkedList<String>> descriptions, HashSet<String> filter, boolean force) {
        if(displayed_power == null || !id.equals(displayed_power) || force) {
            for(int i = 0; i < description_blocks_list.size(); i++)
                description_blocks_list.get(i).setActive(false);
            
            if(descriptions != null) {
                int i = 0;
                for(LinkedList<String> description : descriptions) {
                    description_blocks_list.get(i).setText((LinkedList<String>)description.clone(), filter);
                    description_blocks_list.get(i).setActive(true);
                    i++;
                }
            }
            redraw();
        }
        displayed_power = id;
    }
    
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON1 && e.getSource() == lock && displayed_power != null) {
            Listener.getInstance().powerLock(displayed_power);
        }
    }
    
  //sets the visibility of the lock
    public void setLockVisible(boolean b) {
        lock.setVisible(b);
    }
}
